package com.eee.machine.service.impl;

import com.eee.machine.common.TokenBean;
import com.eee.machine.configuration.StoreConfig;
import com.eee.machine.service.ITokenService;
import com.youzan.open.sdk.client.oauth.model.OAuthToken;
import com.youzan.open.sdk.client.oauth.types.AuthorizationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
public class ITokenServiceImpl implements ITokenService {

    @Autowired
    private TokenBean tokenBean;
    @Autowired
    private StoreConfig storeConfig;
    @Override
    public void getToken(String code) {
        storeConfig.setCode(code);
        AuthorizationCode authorizationCode = new AuthorizationCode(storeConfig.getClientId(),
                storeConfig.getClientSecret(), storeConfig.getRedirectUrl(), storeConfig.getState(), storeConfig.getCode());
        OAuthToken authToken = authorizationCode.getToken();
        Integer expiresIn = authToken.getExpiresIn();
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, expiresIn);
        Date expireTime = instance.getTime();
        tokenBean.setAccessToken(authToken.getAccessToken());
        tokenBean.setRefreshToken(authToken.getRefreshToken());
        tokenBean.setExpireTime(expireTime);
//        tokenBean = new TokenBean(authToken.getAccessToken(), authToken.getRefreshToken(), expireTime);
    }
}
